import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../views/Layout/index.vue'
import Login from '../views/Login/login.vue'
import Home from '../views/Home/Home.vue'
import store from '../store/index'
//异步
const Advert = () => import('../views/Advert/Advert.vue')
const Comm = () => import('../views/Comm/Commodity.vue')
const My = () => import('../views/My/My.vue')
const Params = () => import('../views/Params/Params.vue')
const Packa =()=>import('../views/Params/Packaging/Packa.vue')
const Myback = () => import('../views/My/Myback/Myback.vue')
const Mylist = () => import('../views/My/Mylist/Mylist.vue')
const Myfahuo = ()=>import('../views/My/Myfahuo/Fahuo.vue')
const Myhuanhuo = ()=>import('../views/My/Myhuanhuo/Huanhuo.vue')
const Geren = ()=>import('../views/Geren/Geren.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '',
    component: Layout,
    children: [
      {
        path: '/',
        name: 'Home',
        component: Home,
        // redirect:{name:'/Page'}
      },
      {
        path: '/advert',
        name: 'Advert',
        component: Advert
      },
      {
        path: '/comm',
        name: 'Comm',
        component: Comm
      },
      {
        path:'/geren',
        name:'Geren',
        component:Geren
      },
      {
        path: '/my',
        name: 'My',
        component: My,
        redirect: '/my/my-list',
        children: [
          {
            path: 'my-back',
            component: Myback,
          },
          {
            path: 'my-list',
            component: Mylist,
          },
          {
            path: 'my-fahuo',
            component: Myfahuo,
          },
          {
            path: 'my-huanhuo',
            component: Myhuanhuo,
          }
        ]
      },
      {
        path: '/params',
        name: 'Params',
        component: Params,
        redirect:'/params/packa',
        children:[
          {
            path:'packa',
            component:Packa
          }
        ]
      },
    ],
    meta: {
      isLogin: true
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,

  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, form, next) => {

  if (to.matched.some(record => record.meta.isLogin)) {
    let token = store.state.LoginModule.userinfo.token;
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }

})

export default router

